package com.panshi.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.panshi.bean.Dynamic;
import com.panshi.bean.Message;
import com.panshi.bean.Paging;
import com.panshi.bean.Result;
import com.panshi.config.JWTUtils;
import com.panshi.service.IDynamicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;


@CrossOrigin(maxAge = 3600)
@RestController
@Slf4j
@RequestMapping(value = "/dynamic", produces = {"application/json;charset=UTF-8"})
public class DynamicController {

    @Autowired
    private IDynamicService dynamicService;

    @GetMapping(value = "/personageData")
    public Result getDynamic(int id) {
        Result result= new Result();
        List<Dynamic> dynamic = dynamicService.allQueryDynamic(id);
        if(dynamic.isEmpty()){
            log.info("没有此数据={}", dynamic);
            result.setData(dynamic);
            result.setMsg("没此数据");
            result.setCode(404);
        }else{
            log.info("DynamicController:个人动态数据={}", dynamic);
            result.setData(dynamic);
            result.setMsg("个人动态数据");
            result.setCode(200);
        }
        return result;
    }



    @GetMapping(value = "/MessageData")
    public Result getMessage(HttpServletRequest request, String pagingSize, String CurrenPaging) {
        Result result = new Result();

        // 从请求头中获取token
        String token = request.getHeader("Authorization");
        DecodedJWT decodedJWT = JWTUtils.getTokenInfo(token);
        String userId = decodedJWT.getClaim("id").asString();
        int uId = Integer.valueOf(userId);
        log.info("动态查询个人用户id={}", uId);

        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        PageHelper.startPage(Integer.valueOf(CurrenPaging), Integer.valueOf(pagingSize));
        log.info("当前页={},分页大小={}", Integer.valueOf(CurrenPaging), Integer.valueOf(pagingSize));

        List<Message> message = dynamicService.allQueryMessage(uId);

        //1、PageInfo 是 pagehelper中内值的分页的信息类
        PageInfo info = new PageInfo(message);
        log.info("总条数={}", info.getTotal());
        log.info("显示的数据={}", info.getList());

        if (message.isEmpty()) {
            log.info("没有此数据={}", message);
            result.setData(message);
            result.setMsg("没此数据");
            result.setCode(403);
        } else {
            Message mes= new Message();
            mes.setTotal(info.getTotal());
            message.add(mes);
            log.info("DynamicController:个人动态数据={}", message);
            result.setData(message);
            result.setMsg("个人动态数据");
            result.setCode(200);
        }
        return result;
    }
}
